package com.eiokey.baselibrary.base.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.eiokey.baselibrary.annotation.AnnotationHandler;
import com.eiokey.baselibrary.annotation.BindingVariable;
import com.eiokey.baselibrary.annotation.GetLayoutId;
import com.eiokey.baselibrary.utils.BaseUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.reactivex.disposables.Disposable;

/**
 * description
 * use
 * param
 * return
 * @author JetQiao
 * @date 2021/7/26  16:29
 */

public class BaseFragment<VM extends BaseViewModel, V extends ViewDataBinding> extends Fragment {
    protected List<Disposable> disposables = new ArrayList<>();
    protected VM mViewModel;
    protected V mDataBinding;
    protected View mRootView;
    protected Context mContext;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        beforeBindData(savedInstanceState);
        bindViewModel();
    }

    /**
     * description  获取BR值
     * use
     * param
     * return
     * @author JetQiao
     * @date 2021/7/26  16:31
     */
    private int getBindingVariableId(Object object) {
        if ((object instanceof Activity || object instanceof Fragment || object instanceof Dialog)) {
            Class<?> objClass = object.getClass();
            BindingVariable contentView = objClass.getAnnotation(BindingVariable.class);
            if (contentView != null) {
                return contentView.value();
            }
        }
        return -1;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mDataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), null, false);
        mRootView = mDataBinding.getRoot();
        mDataBinding.setVariable(getBindingVariableId(this), mViewModel);
        initView(savedInstanceState);
        return mRootView;
    }

    /**
     * 设置layoutId,如果使用注解则可以不用重写该方法
     * @return
     */
    protected int setLayoutId() {
        return 0;
    }

    /**
     * 获取layoutId
     * @return
     */
    private int getLayoutId() {
        // 避免反射
        GetLayoutId layoutId = getClass().getAnnotation(GetLayoutId.class);
        if (layoutId != null) {
            return layoutId.value();
        } else {
            return setLayoutId();
        }
    }

    /**
     * 在绑定数据之前，setContentView之前
     * @param bundle
     */
    protected void beforeBindData(Bundle bundle) {

    }

    /**
     * 在绑定数据之后，setContentView之后
     * @param bundle
     */
    protected void initView(Bundle bundle) {
    }

    /**
     * 初始化和绑定viewmodel
     */
    protected void bindViewModel() {
        int layoutId = getLayoutId();
        if (layoutId == 0) {
            throw new RuntimeException("----layoutId can not be null");
        }
        Class<VM> viewModelClass = AnnotationHandler.getViewModelClass(this);
        if (viewModelClass != null) {
            mViewModel = new ViewModelProvider(getViewModelStore(), ViewModelProvider.AndroidViewModelFactory.getInstance(Objects.requireNonNull(getActivity()).getApplication())).get(viewModelClass);
        }

    }

    @Override
    public void onDestroyView() {
        // 取消订阅
        for (Disposable disposable : disposables) {
            BaseUtils.dispose(disposable);
        }
        super.onDestroyView();
    }
}
